/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.siro.pm.PmApplication;
import com.siro.pm.utils.DatabaseOperation;
import com.siro.pm.utils.DatabasePool;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since DemandOrBug.java v1.0
 */
public class DemandOrBug implements Serializable {
	private static final long serialVersionUID = 6953948795968354071L;
	private static final Logger logger = LoggerFactory
			.getLogger(DemandOrBug.class);

	// id, 需求名称，详细描述，所属项目，级别，创建者，指派给谁，状态，审核者，审核结果,创建时间，关闭时间
	private String id;
	private String name;
	private String desc;
	private Project project;
	private String level;
	private Developer creator;
	private Developer solver;
	private String status;
	private Developer advisor;
	private String result;
	private String createtime;
	private String closetime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public Developer getCreator() {
		return creator;
	}

	public void setCreator(Developer creator) {
		this.creator = creator;
	}

	public Developer getSolver() {
		return solver;
	}

	public void setSolver(Developer solver) {
		this.solver = solver;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Developer getAdvisor() {
		return advisor;
	}

	public void setAdvisor(Developer advisor) {
		this.advisor = advisor;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getCreatetime() {
		return createtime;
	}

	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}

	public String getClosetime() {
		return closetime;
	}

	public void setClosetime(String closetime) {
		this.closetime = closetime;
	}

	public static boolean couldEditBug(DemandOrBug bug) {
		String logindUserId = ((Developer) PmApplication.getDefault().getUser())
				.getId();
		return logindUserId.equals(bug.getCreator().getId())
				|| logindUserId.equals(bug.getSolver().getId())
				|| logindUserId.equals(bug.getAdvisor().getId());
	}

	public static void insertDemandOrBug(DemandOrBug bug) {
		logger.info("添加需求或BUG，id:{},name:{}", bug.getId(), bug.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO siropm_bug VALUES('");
		sql.append(bug.getId()).append("','");
		sql.append(bug.getName()).append("','");
		sql.append(bug.getDesc()).append("','");
		sql.append(bug.getProject().getId()).append("','");
		sql.append(bug.getLevel()).append("','");
		sql.append(bug.getCreator().getId()).append("','");
		sql.append(bug.getSolver().getId()).append("','");
		sql.append(bug.getStatus()).append("','");
		sql.append(bug.getAdvisor().getId()).append("','");
		sql.append(bug.getResult()).append("','");
		sql.append(bug.getCreatetime()).append("','");
		sql.append(bug.getClosetime()).append("')");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("添加需求或BUG，id:{},name:{} 成功", bug.getId(), bug.getName());
	}

	public static void deleteDemandOrBug(DemandOrBug bug) {
		logger.info("删除需求或BUG，id:{},name:{}", bug.getId(), bug.getName());
		String sql = "DELETE FROM siropm_bug WHERE id='" + bug.getId() + "'";
		try {
			DatabaseOperation
					.excuteUpdateSQL(DatabasePool.getConnection(), sql);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("删除需求或BUG，id:{},name:{} 成功", bug.getId(), bug.getName());
	}

	public static void updateDemandOrBug(DemandOrBug bug) {
		logger.info("更新需求或BUG，id:{} name:{}", bug.getId(), bug.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE siropm_bug SET ");
		sql.append("name='").append(bug.getName()).append("', ");
		sql.append("description='").append(bug.getDesc()).append("', ");
		sql.append("projectid='").append(bug.getProject().getId()).append("',");
		sql.append("level='").append(bug.getLevel()).append("',");
		sql.append("creatorid='").append(bug.getCreator().getId()).append("',");
		sql.append("solverid='").append(bug.getSolver().getId()).append("',");
		sql.append("status='").append(bug.getStatus()).append("',");
		sql.append("advisorid='").append(bug.getAdvisor().getId()).append("',");
		sql.append("result='").append(bug.getResult()).append("',");
		sql.append("createtime='").append(bug.getCreatetime()).append("',");
		sql.append("closetime='").append(bug.getClosetime()).append("'");
		sql.append("WHERE id='").append(bug.getId()).append("'");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("更新需求或BUG，id:{} name:{} 成功", bug.getId(), bug.getName());
	}

	public static List<DemandOrBug> getAllDemandOrBugs() {
		logger.info("获取所有需求或BUG...");
		List<DemandOrBug> demandOrBugs = new ArrayList<DemandOrBug>();
		String sql = "SELECT id,name,description,projectid,level,creatorid,solverid,status,advisorid,result,createtime,closetime FROM siropm_bug";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				DemandOrBug demandOrBug = new DemandOrBug();
				demandOrBug.setId(row.get(0));
				demandOrBug.setName(row.get(1));
				if (row.get(2) != null && !row.get(2).equals(""))
					demandOrBug.setDesc(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					demandOrBug.setProject(Project.getProject(row.get(3)));
				if (row.get(4) != null && !row.get(4).equals(""))
					demandOrBug.setLevel(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					demandOrBug.setCreator(Developer.getDeveloper(row.get(5)));
				if (row.get(6) != null && !row.get(6).equals(""))
					demandOrBug.setSolver(Developer.getDeveloper(row.get(6)));
				if (row.get(7) != null && !row.get(7).equals(""))
					demandOrBug.setStatus(row.get(7));
				if (row.get(8) != null && !row.get(8).equals(""))
					demandOrBug.setAdvisor(Developer.getDeveloper(row.get(8)));
				if (row.get(9) != null && !row.get(9).equals(""))
					demandOrBug.setResult(row.get(9));
				if (row.get(10) != null && !row.get(10).equals(""))
					demandOrBug.setCreatetime(row.get(10));
				if (row.get(11) != null && !row.get(11).equals(""))
					demandOrBug.setClosetime(row.get(11));
				demandOrBugs.add(demandOrBug);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return demandOrBugs;
	}

	public static DemandOrBug getDemandOrBug(String id) {
		logger.info("获取需求或BUG,ID:" + id);
		DemandOrBug demandOrBug = null;
		String sql = "SELECT id,name,description,projectid,level,creatorid,solverid,status,advisorid,result,createtime,closetime FROM siropm_bug WHERE id='"
				+ id + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			if (rslt == null || rslt.size() == 0)
				return null;
			List<String> row = rslt.get(1);
			demandOrBug = new DemandOrBug();
			demandOrBug.setId(row.get(0));
			demandOrBug.setName(row.get(1));
			if (row.get(2) != null && !row.get(2).equals(""))
				demandOrBug.setDesc(row.get(2));
			if (row.get(3) != null && !row.get(3).equals(""))
				demandOrBug.setProject(Project.getProject(row.get(3)));
			if (row.get(4) != null && !row.get(4).equals(""))
				demandOrBug.setLevel(row.get(4));
			if (row.get(5) != null && !row.get(5).equals(""))
				demandOrBug.setCreator(Developer.getDeveloper(row.get(5)));
			if (row.get(6) != null && !row.get(6).equals(""))
				demandOrBug.setSolver(Developer.getDeveloper(row.get(6)));
			if (row.get(7) != null && !row.get(7).equals(""))
				demandOrBug.setStatus(row.get(7));
			if (row.get(8) != null && !row.get(8).equals(""))
				demandOrBug.setAdvisor(Developer.getDeveloper(row.get(8)));
			if (row.get(9) != null && !row.get(9).equals(""))
				demandOrBug.setResult(row.get(9));
			if (row.get(10) != null && !row.get(10).equals(""))
				demandOrBug.setCreatetime(row.get(10));
			if (row.get(11) != null && !row.get(11).equals(""))
				demandOrBug.setClosetime(row.get(11));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return demandOrBug;
	}

	public static List<DemandOrBug> getDemandOrBugs(Project project) {
		logger.info("获取与工程:{} 相关的所有需求或BUG...", project.getName());
		List<DemandOrBug> demandOrBugs = new ArrayList<DemandOrBug>();
		String sql = "SELECT id,name,description,projectid,level,creatorid,solverid,status,advisorid,result,createtime,closetime FROM siropm_bug WHERE projectid='"
				+ project.getId() + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				DemandOrBug demandOrBug = new DemandOrBug();
				demandOrBug.setId(row.get(0));
				demandOrBug.setName(row.get(1));
				if (row.get(2) != null && !row.get(2).equals(""))
					demandOrBug.setDesc(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					demandOrBug.setProject(project);
				if (row.get(4) != null && !row.get(4).equals(""))
					demandOrBug.setLevel(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					demandOrBug.setCreator(Developer.getDeveloper(row.get(5)));
				if (row.get(6) != null && !row.get(6).equals(""))
					demandOrBug.setSolver(Developer.getDeveloper(row.get(6)));
				if (row.get(7) != null && !row.get(7).equals(""))
					demandOrBug.setStatus(row.get(7));
				if (row.get(8) != null && !row.get(8).equals(""))
					demandOrBug.setAdvisor(Developer.getDeveloper(row.get(8)));
				if (row.get(9) != null && !row.get(9).equals(""))
					demandOrBug.setResult(row.get(9));
				if (row.get(10) != null && !row.get(10).equals(""))
					demandOrBug.setCreatetime(row.get(10));
				if (row.get(11) != null && !row.get(11).equals(""))
					demandOrBug.setClosetime(row.get(11));
				demandOrBugs.add(demandOrBug);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return demandOrBugs;
	}

	public static List<DemandOrBug> getDemandOrBugs(Developer developer) {
		logger.info("获取与开发者:{} 相关的所有需求或BUG...", developer.getName());
		List<DemandOrBug> demandOrBugs = new ArrayList<DemandOrBug>();
		String sql = "SELECT id,name,description,projectid,level,creatorid,solverid,status,advisorid,result,createtime,closetime FROM siropm_bug WHERE creatorid='"
				+ developer.getId()
				+ "' OR solverid='"
				+ developer.getId()
				+ "' OR advisorid='" + developer.getId() + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				DemandOrBug demandOrBug = new DemandOrBug();
				demandOrBug.setId(row.get(0));
				demandOrBug.setName(row.get(1));
				if (row.get(2) != null && !row.get(2).equals(""))
					demandOrBug.setDesc(row.get(2));
				if (row.get(3) != null && !row.get(3).equals(""))
					demandOrBug.setProject(Project.getProject(row.get(3)));
				if (row.get(4) != null && !row.get(4).equals(""))
					demandOrBug.setLevel(row.get(4));
				if (row.get(5) != null && !row.get(5).equals(""))
					demandOrBug.setCreator(Developer.getDeveloper(row.get(5)));
				if (row.get(6) != null && !row.get(6).equals(""))
					demandOrBug.setSolver(Developer.getDeveloper(row.get(6)));
				if (row.get(7) != null && !row.get(7).equals(""))
					demandOrBug.setStatus(row.get(7));
				if (row.get(8) != null && !row.get(8).equals(""))
					demandOrBug.setAdvisor(Developer.getDeveloper(row.get(8)));
				if (row.get(9) != null && !row.get(9).equals(""))
					demandOrBug.setResult(row.get(9));
				if (row.get(10) != null && !row.get(10).equals(""))
					demandOrBug.setCreatetime(row.get(10));
				if (row.get(11) != null && !row.get(11).equals(""))
					demandOrBug.setClosetime(row.get(11));
				demandOrBugs.add(demandOrBug);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return demandOrBugs;
	}
}
